import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('charts'),
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            Container(
              height: 301,
              child: getBar(),
            ),
            Container(
              height: 301,
              child: getLine(),
            ),
          ],
        ),
      ),
    );
  }



  Widget getLine() {
    List<LineSales> dataLine = [
      LineSales(DateTime(2019, 7, 2), 33),
      LineSales(DateTime(2019, 7, 3), 55),
      LineSales(DateTime(2019, 7, 4), 22),
      LineSales(DateTime(2019, 7, 5), 88),
      LineSales(DateTime(2019, 7, 6), 123),
      LineSales(DateTime(2019, 7, 7), 75),
    ];

    var seriesLine = [
        charts.Series(
          data: dataLine,
          domainFn: (LineSales lines, _) => lines.time,
          measureFn: (LineSales lines, _) => lines.sale,
          id:  "Lines",
        )
    ];

    //是TimeSeriaesCharts，而不是LineChart，因为x轴是DataTime类
    Widget line = charts.TimeSeriesChart(seriesLine);
    //line = charts.LineCharts(serires);
    return line;
  }


  //获取柱状图
  Widget getBar() {
    List<Barsales> dataBar = [
      Barsales("1", 20),
      Barsales("2", 50),
      Barsales("3", 20),
      Barsales("4", 80),
      Barsales("5", 120),
      Barsales("6", 30),
    ];


    var seriesBar = [
      charts.Series(
        data: dataBar,
        domainFn: (Barsales sale, _) => sale.day,
        measureFn: (Barsales sale, _) => sale.sale,
        id: "Sales",
      )
    ];

    return charts.BarChart(seriesBar);
  }

}

class Barsales {
  String day;
  int sale;

  Barsales(this.day, this.sale);
}

class LineSales {
  DateTime time;
  int sale;

  LineSales(this.time, this.sale);
}


